import { queryCcs, create, again } from '@/api/approval/approve'

export default {
  props: {
    info: {
      type: Object,
      default: null,
    },
    getData: {
      type: Function,
      default: function () {},
    },
  },
  inject: ['app'],
  provide() {
    return {
      app: this,
    }
  },
  data() {
    return {
      ccs: '',
      companys: [
        '里欧总公司',
        '江苏里欧水务',
        '科涤',
        '溪岸雅阁',
        '森邻百货',
        '绿能空调',
      ],
      rates: [
        '0%',
        '1%',
        '3%',
        '5%',
        '6%',
        '9%',
        '10%',
        '11%',
        '13%',
        '16%',
        '17%',
      ],
      invoice_types: ['增值税普通发票', '增值税专用发票', '其他'],
      reimbursement_types: [
        '交通费',
        '住宿费',
        '办公文件费',
        '申请费',
        '报关费',
        '生产安装用物料',
        '物料运输费',
        '其他费用',
      ],
      pay_types: ['支票', '贷记', '电汇', '汇票', '现金', '银行卡', '其他'],
      basiss: [
        '合同',
        '采购单',
        '签回单（送货单）正本',
        '订购单',
        '发票',
        '图纸',
      ],
      overtime_types: ['调休', '计薪'],
      overtime_parts: ['工作日加班', '周末加班', '法定节假日加班'],
      leave_types: [
        '年假',
        '事假',
        '病假',
        '调休',
        '婚假',
        '产假',
        '陪产假',
        '丧假',
      ],
      regions: ['国内一线城市', '国内二线城市', '国内三线城市'],
      yn: ['否', '是'],
      seals: ['合同章', '公章', '法人章', '其他'],
      form: {},
      visible: false,
    }
  },
  methods: {
    validate() {
      return new Promise(async (resolve, reject) => {
        this.$refs['formDate'].validate(async (valid) => {
          if (valid) {
            resolve()
          } else {
            reject()
          }
        })
      })
    },
    createForms(e) {
      const forms = []
      const len = e.length
      let i = 0
      for (i = 0; i < len; i++) {
        const f = {}
        f.t = e[i][0]
        f.v = e[i][1]
        f.k = e[i][2]
        f.c = e[i][3]
        forms.push(f)
      }
      return forms
    },
    toFix(v, n) {
      if (v) {
        return Number(v).toFixed(n)
      }
      return Number(0).toFixed(n)
    },
    queryCcs() {
      this.$set(this, 'ccs', '')
      queryCcs({
        approve_key: this.approve_key,
      })
        .then((response) => {
          let ccs = response.list
          this.$set(this, 'ccs', JSON.stringify(ccs))
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    initApply(e) {
      again(e)
        .then((response) => {
          let forms = response.forms
          if (forms && forms !== '') {
            for (let key in forms) {
              if (
                key === 'pro_id' ||
                key === 'pur_id' ||
                key === 'sup_id' ||
                isNaN(Number(forms[key]))
              ) {
                this.$set(this.form, key, forms[key])
              } else {
                this.$set(this.form, key, Number(forms[key]))
              }
            }
          }
          let files = response.files
          if (files && files !== '') this.$set(this.form, 'files', files)
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    handleCreate(e) {
      create(e)
        .then((response) => {
          if (this.getData) this.getData()
          this.handleCancel()
          this.$message.success('提交成功')
        })
        .catch((err) => {
          this.$message.error(err.msg)
        })
    },
    handleCancel(done) {
      if (this.$refs['formDate']) this.$refs['formDate'].resetFields()
      if (done && typeof done === Function) {
        done()
      } else {
        this.visible = false
      }
    },
  },
}
